<?php

namespace app\store\controller;

use app\store\service\LoginService;
use app\store\service\UserService;
use app\BaseController;
use app\Response;
use think\facade\Request;

class User extends BaseController
{
    /** 用户中心
     */
    public function userCenter(){
        if (Request::isPost()) {
            $user_id = LoginService::getUserIdFromToken();
            if (!is_numeric($user_id)) {
                return $user_id;
            }
            $res = UserService::userCenterService($user_id);
            return $res;
        }
    }


    public function userEdit(){
        $user_id = LoginService::getUserIdFromToken();
        if(!is_numeric($user_id)){
            return $user_id;
        }
        $nickname = Request::param("nickname");
        $photo = Request::param("photo");
        $province = Request::param("province");
        $city = Request::param("city");
        $data = [
            "nickname" => $nickname,
            "photo" => $photo,
            "province" => $province,
            "city" => $city,
        ];
        $res = UserService::editUserInfoService($user_id,$data);
        return $res;
    }


    public function editPassword(){
        if (Request::isPost()) {
            $user_id = LoginService::getUserIdFromToken();
            if (!is_numeric($user_id)) {
                return $user_id;
            }
            $passwordOld = Request::post("passwordOld","");
            $password = Request::post("password","");
            $type = Request::post("type",1);
            if($type == 2){
                if(empty($passwordOld)||empty($password)){
                    return Response::wrapError(0,"请按要求填写新旧密码!");
                }
            }else{
                if(empty($password)){
                    return Response::wrapError(0,"请填写密码!");
                }
            }

            $res = LoginService::userEditPasswordService($user_id,$passwordOld,$password);
            return $res;
        }
    }



}